`
https://leetcode.cn/problems/all-paths-from-source-to-target/
`

/**
 * @param {number[][]} graph
 * @return {number[][]}
 */
var allPathsSourceTarget = function (graph) {
  // 记录所有路径
  const res = []
  const path = []
  const n = graph.length

  // 图的遍历框架
  const traverse = (val) => {
    // 添加节点 val 到路径
    path.push(val)

    if (val === n - 1) {
      // 到达终点
      res.push(path.slice())
      path.pop()
      return
    }

    // 递归每个相邻节点
    for (const v of graph[val]) {
      traverse(v)
    }

    // 从路径移出节点 val
    path.pop()
  }

  traverse(0)
  return res
};